package org.sam.swing.table.renderer;

import java.util.Collection;

import javax.swing.table.DefaultTableCellRenderer;

import org.sam.swing.utils.ReflectUtil;

/**
 * 显示集合字段内容
 * @author sam
 *
 */
public class JSTableCollectionEntityFieldRenderer extends DefaultTableCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7029817775617565094L;

	/**
	 * 实体对象名称
	 */
	private String fieldName = "";

	/**
	 * 实体对象名称
	 * 
	 * @return
	 */
	public String getFieldName() {
		return fieldName;
	}

	/**
	 * 实体对象名称
	 * 
	 * @param fieldName
	 */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	/**
	 * 使用实体字段方式显示的渲染器
	 * 
	 * @param fieldName
	 */
	public JSTableCollectionEntityFieldRenderer(String fieldName) {
		super();
		this.setFieldName(fieldName);
	}

	/**
	 * 重写的赋值部分
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected void setValue(Object value) {
		if (value != null) {
			StringBuilder sb = new StringBuilder();
			Collection c = (Collection) value;
			sb.append("[");
			int size = c.size();
			int i = 1;
			for (Object obj : c) {
				sb.append((ReflectUtil.getDisplay(obj, fieldName)));
				if (i < size) {
					sb.append(",");
				}
				i++;
			}
			sb.append("]");
			setText(sb.toString());
		} else {
			setText("");
		}
	}
}
